<form method="post" action="/oss/?r=activity/doAdd">
    <{include file='../common_params.tpl'}>

    <div class="box box-black">
        <div class="box-body">
            <h5> <i class="fa fa-users"></i>&nbsp;&nbsp;活动基础信息</h5>
            <table class="table table-bordered">
                <colgroup>
                    <col style="width: 20%;"></col>
                    <col style="width: 80%;"></col>
                </colgroup>

                <tr>
                    <th class="th-label">类别</th>
                    <td>
                        <select class="form-control inline" name="classify">
                        	<option value="">请选择</option>
                        	<{foreach from=$classifyList item=item key=key}>
                        		<option value="<{$key}>"><{$item}></option>
                        	<{/foreach}>
                        </select>                       
                    </td>
                </tr>
                
                <tr>
                    <th class="th-label">活动</th>
                    <td>
                        <select class="form-control inline" name="category_id">
                        	<option value="">请选择</option>
                        </select>
                        <div style="margin-top: 10px;display: none;" id="ac_id_name1">
                        	活动ID：<span class="ac_id"></span>
                        </div>
                        <div style="margin-top: 5px;display: none;" id="ac_id_name2">
                        	活动名称：<span class="ac_name"></span>
                        </div>
                                                               
                    </td>
                </tr>
                
                <tr>
                    <th class="th-label">开始时间</th>
                    <td>
                        <input type="text" class="form-control inline range-input time" name="start_time" placeholder="开始时间" />                       
                    </td>
                </tr>
                
                <tr>
                    <th class="th-label">活动范围</th>
                    <td>
                        开服大于&nbsp;<input type="number" class="form-control inline" style="width: 60px;" name="postpone" value="2" placeholder="" />&nbsp;天开启                       
                    </td>
                </tr>
                

                

            </table>
            
            
        </div>
        
        <div class="box-footer">
            <a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
            <button type="submit" name="sub" class="btn btn-default pull-right">保存</button>
        </div>
        
    </div>
</form>

<script type="text/javascript">
	
    $(function(){
        $("input.time").datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
        });

        $(".range-input").change(function(){
            var object = $(this),
                index = object.index();

            if(index == 1){
                var from = getRangeVal(object.prev());
                var to = getRangeVal(object);
            }else {
                var from = getRangeVal(object);
                var to = getRangeVal(object.next());
            }

            if(to && from && to < from){
                alert('范围填写错误!');
                object.val('');
            }
        });

        function getRangeVal(object){
            if(!object) return null;

            var val = object.val();

            if(object.hasClass('time')){
                return Date.parse(new Date(val));
            }else{
                return val;
            }
        }
        
        
        $(document).on('change','select[name = "classify"]',function(){
        	var g = $('input[name="g"]').val();
	        var url = '?r=activity/getActivity&g='+g;
        	
        	var classify = $(this).val();
        	if(classify == ''){
				var option = '';	
				option += "<option value=''>请选择</option>";
				$('select[name="category_id"]').empty();
				$('select[name="category_id"]').append(option);
				$('#ac_id_name1').hide();
        		$('#ac_id_name2').hide();
				$(".ac_id").text('');
				$(".ac_name").text('');
				return false;
			}
        	
          	$.ajax({
	        	type : 'POST',
	        	url : url,
	        	async:false,
	        	data : {'classify':classify},
		        dataType :'JSON',
		        success : function(res){
		        	$('#ac_id_name1').hide();
        		    $('#ac_id_name2').hide();
				    $(".ac_id").text('');
				    $(".ac_name").text('');
		        	if(res.status){	
		        		var option = '';	
					    option += "<option value=''>请选择</option>";
					    for (var i=0;i<res.data.length;i++){
					    	var selected = '';
						    option += "<option "+selected+" value='"+res.data[i]['id']+"'>"+res.data[i]['activity_name']+"</option>";
					    }
					    $('select[name="category_id"]').empty();
					    $('select[name="category_id"]').append(option);
					   //layer.alert('设置成功', {icon: 1, skin: 'layui-layer-molv'});
			        }else{
			        	//layer.alert('设置失败', {icon: 2, skin: 'layui-layer-molv'});
			        	var option = '';	
					    option += "<option value=''>请选择</option>";
					    $('select[name="category_id"]').empty();
					    $('select[name="category_id"]').append(option);
			        }
		        }
	        });
        });
        
        $(document).on('change','select[name = "category_id"]',function(){
        	var g = $('input[name="g"]').val();
	        var url = '?r=activity/getActivityInfo&g='+g;
        	
        	var category_id = $(this).val();
        	if(category_id == ''){
        		layer.msg('请选择活动', {icon: 2, skin: 'layui-layer-molv'});
        		$('#ac_id_name1').hide();
        		$('#ac_id_name2').hide();
				$(".ac_id").text('');
				$(".ac_name").text('');
				return false;
			}
        	
        	$.ajax({
	        	type : 'POST',
	        	url : url,
	        	async:false,
	        	data : {'category_id':category_id},
		        dataType :'JSON',
		        success : function(res){
		        	if(res.status){
		        		$('#ac_id_name1').show();
		        		$(".ac_id").text(res.data.activity_id);
		        		$('#ac_id_name2').show();
		        		$(".ac_name").text(res.data.activity_name);
		        		
			        }else{
			        	layer.alert(res.msg, {icon: 2, skin: 'layui-layer-molv'});
			        	$('#ac_id_name1').hide();
        		        $('#ac_id_name2').hide();
				        $(".ac_id").text('');
				        $(".ac_name").text('');
			        }
		        }
	        });
        	
        });
    	
        
        $('button[name="sub"]').click(function(){
    		var classify = $('select[name="classify"]').val();
        	if(classify == ''){
        		layer.alert('请选择类别', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        	
    		var category_id = $('select[name="category_id"]').val();
        	if(category_id == ''){
        		layer.alert('请选择活动', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        	
        	var start_time = $('input[name="start_time"]').val();
        	if(start_time == ''){
        		layer.alert('请选择开始时间', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        	
        	var postpone = $('input[name="postpone"]').val();
        	if(postpone == ''){
        		layer.alert('请填写活动范围', {icon: 2, skin: 'layui-layer-molv'});
            	return false;
        	}
        });
        
        
    });
</script>